package com.zheng;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;
import net.sf.excelutils.ExcelException;
import net.sf.excelutils.ExcelUtils;
import org.dom4j.Element;
import tangdi.engine.context.Etf;
import tangdi.engine.context.Log;
import tangdi.engine.context.Msg;

public class Importcustommxq
{
  @Named("ToReportcustomxq")
  public static int ToReportcustomxq(@Named("Root") String Root, @Named("FileDemo") String FileDemo, @Named("FilePath") String FilePath, @Named("FileToPath") String FileToPath, @Named("NameForUser") String NameForUser, @Named("WebPath") String WebPath)
  {
    List reportList = null;

    reportList = iterlist2(Root);
    ExcelUtils.addValue("reportList", reportList);
    String config = WebPath + "/" + FilePath + "/" + FileDemo + ".xls";
    Log.info("config", new Object[] { config });
    String fileName = NameForUser + System.currentTimeMillis() + ".xls";
    Log.info("----fileName=%s--------", new Object[] { WebPath + FileToPath + "/" + fileName + config });
    try {
      ExcelUtils.export(config, new FileOutputStream(WebPath + FileToPath + "/" + fileName));
    }
    catch (FileNotFoundException e) {
      Log.info("----fileName=%s--", new Object[] { WebPath + FileToPath + "/" + fileName + "没找到" });
      return 1;
    }
    catch (ExcelException e) {
      Log.info("----fileName=%s--", new Object[] { WebPath + FileToPath + "/" + fileName + "导入异常" });
      return 2;
    }
    Msg.setTmp("FILENAME", fileName);
    return 0;
  }
  public static List iterlist2(String ROOT) {
    List list = Etf.childs(ROOT);
    Log.info("list的长度=%s", new Object[] { Integer.valueOf(list.size()) });
    List reportList = new ArrayList();

    for (int i = 0; i < list.size(); i++) {
      Log.info("----start=%s-----", new Object[] { "shabi" });
      
      Element em = (Element)list.get(i);
      Log.info( em.element("ORDAMT").getText(), new Object[] { "shabi" });
      Customxq user = new Customxq();
      user.setStlbatno(em.element("STLBATNO") == null ? "无记录" : em.element("STLBATNO").getText());
      user.setMerno(em.element("MERNO") == null ? "无记录" : em.element("MERNO").getText());
      user.setCust_name(em.element("CUST_NAME") == null ? "无记录" : em.element("CUST_NAME").getText());
      user.setPrdordno(em.element("PRDORDNO") == null ? "无记录" : em.element("PRDORDNO").getText());
      user.setPrdordtype(em.element("PRDORDTYPE") == null ? "无记录" : em.element("PRDORDTYPE").getText());
      user.setPayordno(em.element("PAYORDNO") == null ? "无记录" : em.element("PAYORDNO").getText());
      user.setOrdamt(em.element("ORDAMT") == null ? "无记录" : em.element("ORDAMT").getText());
      user.setComamt(em.element("COMAMT") == null ? "无记录" : em.element("COMAMT").getText());
      user.setOrdertime(em.element("ORDERTIME") == null ? "无记录" : em.element("ORDERTIME").getText());
      user.setAday(em.element("ADAY") == null ? "无记录" : em.element("ADAY").getText());
      user.setTime(em.element("TIME") == null ? "无记录" : em.element("TIME").getText());

      reportList.add(user);
    }
    return reportList;
  }
}